package user_interaction;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.NoSuchElementException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import dca.BadDataException;
import dca.Doctor;
import dca.Patient;
import dca.PatientRegistry;
import dca.Symptom;
import dca.DiseaseRegistry;
import dca.SymptomRegistry;


public class Patient_GUI extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int FRAME_WIDTH = 600;
	private static final int FRAME_HEIGTH = 500;
	
	private JTextArea display;
	private JScrollPane displayScrollpane;
	private JTextField nameField;
	private JTextField healthField;
	private JComboBox<?> symptom1;
	private JComboBox<?> symptom2;
	private JComboBox<?> symptom3;
	private JComboBox<?> symptom4;
	private JComboBox<?> symptom5;
	private JComboBox<?> symptom6;
	private JComboBox<?> symptom7;
	private JComboBox<?> symptom8;	
	private JButton AddButton;
	private JButton CureButton;
	
	private PatientRegistry PatientBase;
	private SymptomRegistry SymptomBase;
	private Doctor drDiagnose;
	
	public Patient_GUI(PatientRegistry PatientBase,SymptomRegistry SymptomBase,DiseaseRegistry DiseaseBase){
		this.PatientBase = PatientBase;
		this.SymptomBase = SymptomBase;
		
		try {
			this.drDiagnose = new Doctor(DiseaseBase.getFilename());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		
		//add visual components
		
		display = new JTextArea(15,50);
		display.setToolTipText("Main display");
		displayScrollpane = new JScrollPane(display);		
		
		nameField = new JTextField(10);
		nameField.setToolTipText("Patient name");
		
		healthField = new JTextField(2);
		healthField.setToolTipText("Patient health");
		
		AddButton = new JButton("Add Patient");
		CureButton = new JButton("Attempt Cure");

		
		//Finds the existing symptoms for dropDown boxes
		String [] symptomsString = new String[SymptomBase.SymptomList().size()+1];
		symptomsString[0]="N/A";
		int i=1;
		for (Symptom s: SymptomBase.SymptomList())
		{
			symptomsString[i]=s.getName();
			i++;
		}		
		symptom1 = new JComboBox<Object>(symptomsString);
		symptom2 = new JComboBox<Object>(symptomsString);
		symptom3 = new JComboBox<Object>(symptomsString);
		symptom4 = new JComboBox<Object>(symptomsString);
		symptom5 = new JComboBox<Object>(symptomsString);
		symptom6 = new JComboBox<Object>(symptomsString);
		symptom7 = new JComboBox<Object>(symptomsString);
		symptom8 = new JComboBox<Object>(symptomsString);
		
		AddButton.addActionListener(new AddButtonListener());
		CureButton.addActionListener(new CureButtonListener());
		setLayout(new FlowLayout());
		setSize(FRAME_WIDTH,FRAME_HEIGTH);
		add(displayScrollpane);
		add(nameField);
		add(healthField);
		add(symptom1);
		add(symptom2);
		add(symptom3);
		add(symptom4);
		add(symptom5);
		add(symptom6);
		add(symptom7);
		add(symptom8);
		add(AddButton);
		add(CureButton);
		

		//Add content to display
			updateDisplay();
	
		
	}
	
	public void updateDisplay(){
		String PatientList="";		
		for(Patient p:PatientBase.PatientList())
		{
			PatientList+=p.toString()+"\n";			
			try{
			PatientList+="Suggested cure: "+drDiagnose.diagnose(p).getCure().toString()+"\n\n";
			}
			catch(NoSuchElementException e){
				PatientList+="No diagnosis\n\n";
			}
			
		}	
		display.setText(PatientList);
		
	}
	
	class CureButtonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			for(Patient p:PatientBase.PatientList()){
				try {
					drDiagnose.diagnose(p).getCure().administer(p);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}catch(NoSuchElementException e){
					System.out.println(e.getMessage());
				}
			}
			try {
				PatientBase.savePatients();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			updateDisplay();
		}
		
	}
	
	class AddButtonListener implements ActionListener{

		@Override		
		public void actionPerformed(ActionEvent arg0)  {
			try {
			String name = nameField.getText();
			int		Health = Integer.parseInt(healthField.getText().trim());	
			ArrayList<Symptom> symptoms = new ArrayList<Symptom>();
			if(symptom1.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom1.getSelectedItem().toString()));
			if(symptom2.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom2.getSelectedItem().toString()));
			if(symptom3.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom3.getSelectedItem().toString()));
			if(symptom4.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom4.getSelectedItem().toString()));
			if(symptom5.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom5.getSelectedItem().toString()));
			if(symptom6.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom6.getSelectedItem().toString()));
			if(symptom7.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom7.getSelectedItem().toString()));
			if(symptom8.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom8.getSelectedItem().toString()));
			
			
				Patient aPatient = new Patient(name,Health,symptoms);
				PatientBase.addPatient(aPatient);
				PatientBase.savePatients();
				updateDisplay();
			} catch (FileNotFoundException e) {
				System.out.println(e.getMessage());
			} catch (BadDataException e) {				
				System.out.println(e.getMessage());
			} catch (NumberFormatException e){
				System.out.println(e.getMessage());
			}
			
			
			 
			
		}
		
	}
	

}
